package com.cash.channel.svc.service.common;

import com.cash.channel.svc.dao.ThirdIdMappingMapper;
import com.cash.channel.svc.logic.RepayLogic;
import com.alibaba.fastjson.JSON;
import com.cash.channel.api.constant.Constant;
import com.cash.channel.api.model.ApiRequestModel;
import com.cash.channel.api.model.ApiResponseModel;
import com.cash.channel.api.model.repay.WdOrderInfoModel;
import com.cash.channel.api.model.request.RepayDetailRequest;
import com.cash.channel.api.model.response.RepayDetailResponse;
import com.cash.channel.api.model.user.ThirdIdModel;
import com.cash.channel.svc.service.base.RepayDetail;
import geex.core.service.LoanService;
import geex.core.service.model.loan.RepayPlan;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Service(Constant.CHANNEL.COMMON + Constant.SERVICE.查询还款详情)
public class CommonRepayDetail implements RepayDetail {

    @Autowired
    private ThirdIdMappingMapper thirdIdMappingMapper;

    @Autowired
    private RepayLogic repayLogic;

    @Autowired
    private LoanService loanService;

    @Override
    public ApiResponseModel<RepayDetailResponse> doRepayDetail(ApiRequestModel apiRequestModel) {
        RepayDetailRequest  repayDetailRequest = JSON.parseObject(apiRequestModel.getData(), RepayDetailRequest.class);
        String orderNo = repayDetailRequest.getOrderNo();
        Integer period = repayDetailRequest.getPeriod();
        RepayDetailResponse response =new RepayDetailResponse();
        ThirdIdModel thirdIdModel = thirdIdMappingMapper.getThirdIdByThirdIdAndChannel(orderNo, apiRequestModel.getChannel());
        log.info("getCommonRepayInfo thirdIdModel:" + thirdIdModel);
        if (thirdIdModel == null || StringUtils.isEmpty(thirdIdModel.getCUserUid())) {
            return new ApiResponseModel<>(false,0,"未找到用户信息",null);
        }
        if (StringUtils.isEmpty(repayDetailRequest.getWithdrawId())) {
            return new ApiResponseModel<>(false,0,"未找到提现订单号",null);
        }


        WdOrderInfoModel wdOrderInfoModel = this.repayLogic.findWithdrawAppInfoByAppId(repayDetailRequest.getWithdrawId());
        if (wdOrderInfoModel == null || StringUtils.isEmpty(wdOrderInfoModel.getCWdAppId())) {
            return new ApiResponseModel<>(false,0,"未找到提现订单信息",null);
        }
        log.info("查询核心还款明细：" + wdOrderInfoModel.getCWdAppId());
        List<RepayPlan> repayPlanList = loanService.getRepayPlanByExtId(wdOrderInfoModel.getCWdAppId());
        log.info("查询CWdAppId:{}核心还款明细结果{}：",wdOrderInfoModel.getCWdAppId() , repayPlanList);
        if (repayPlanList == null||repayPlanList.size() == 0 || period > repayPlanList.size() || period < 1) {
            return new ApiResponseModel<>(false,0,"期数不正确",null);
        }
        RepayPlan repayPlan=repayPlanList.get(period-1);
        response.setPayAmount(BigDecimal.valueOf(repayPlan.getAllPayAmount()));
        response.setPayCorpus(BigDecimal.valueOf(repayPlan.getAllPayCorpus()));
        response.setPayFee(BigDecimal.valueOf(repayPlan.getAllPayFee()));
        Double lateFee = repayPlan.getAllPayLateFee() + repayPlan.getAllPayPenalties();
        response.setLateFee(BigDecimal.valueOf(lateFee));

        return new ApiResponseModel<>(true,0,"",response);
    }
}
